//require formGetControlValue() function;

function ToolGUI (env, tools)
{
    this.Env = env;
    this.Tools = tools;
    this.tool_id = null;
    this.tool_form = null;
    this.submit_button = null;
    this.form = null;


    this.getCurrentId = function()
    {
        var value = formValueOf(this.tool_form, this.Env.select_tool_control_name);
        return typeof value == 'string' ? parseInt(value) : null;
    }

    this.isSelectedReport = function()
    {
        var value = formValueOf(this.form, this.Env.select_report_control_name);
        return typeof value == 'string';
    }

    this.isSelectedNorm = function()
    {
        var value = formValueOf(this.form, this.Env.select_norm_control_name);
        return typeof value == 'string';
    }

    this.attacheEvent = function()
    {
        var radios = this.tool_form.elements[this.Env.select_tool_control_name];
        if (radios.length == undefined) {
            radios = [radios];
        }
        for (var i = 0; i < radios.length; i++) {
            $(radios[i]).click(function() {
                __ToolGUIEnv.onClick();
            });
        }
    }

    this.onClick = function()
    {
        var current_id = this.getCurrentId();
        if (this.tool_id != current_id) {
            this.tool_id = current_id;
            this.onChange();
        }

        return true;
    }

    this.secondOnClick = function()
    {
        var ff = this.isSelectedReport();
        var dd = false;
        if (this.getTool().type == 'TEST') {
            dd = this.isSelectedNorm();
        } else {
            dd = true;
        }
        this.submit_button.disabled = ! (ff && dd);
        this.setConfirmString();
        return true;
    }

    this.onChange = function()
    {
        this.onDraw();
    }

    this.getReports = function()
    {
        if (this.tool_id != null) {
            for (var i = 0; i < this.Tools.length; i++) {
                if (this.Tools[i].id == this.tool_id) {
                    return this.Tools[i].Test.Reports;
                }
            }
        }

        return null;
    }

    this.getTool = function()
    {
        if (this.tool_id != null) {
            for (var i = 0; i < this.Tools.length; i++) {
                if (this.Tools[i].id == this.tool_id) {
                    return this.Tools[i].Test;
                }
            }
        }

        return null;
    }

    this.getNorms = function()
    {
        if (this.tool_id != null) {
            for (var i = 0; i < this.Tools.length; i++) {
                if (this.Tools[i].id == this.tool_id) {
                    return this.Tools[i].Test.Norms;
                }
            }
        }

        return null;
    }

    this.onDraw = function()
    {
        this.submit_button.disabled = true;
        this.drawReports();
        this.drawNorms();
    }

    this.drawReports = function()
    {
        var rows = this.getReports();
        if (rows != null) {
            $('#reports_section').replaceWith($(this.drawReportsTable(rows)));
        } else {
            $('#reports_section').replaceWith($(this.drawReportsEmpty()));
        }
    }

    this.drawNorms = function()
    {
        var rows = this.getNorms();
        if (rows != null) {
            $('#norms_section').replaceWith($(this.drawNormsTable(rows)));
        } else {
            $('#norms_section').replaceWith($(this.drawNormsEmpty()));
        }
    }

    this.drawReportsEmpty = function()
    {
        var result = $(document.createElement('tbody'));

        result
            .attr('id', 'reports_section')
            .append(document.createElement('tr')).children()
                .attr('class', 'sys_clear')
                .append(document.createElement('td')).children()
                    .attr('class', 'form-element-container sys_clear')
                    .append(document.createElement('div')).children()
                        .attr('class', 'form-element')
        return result;
    }

    this.drawNormsEmpty = function()
    {
        var result = $(document.createElement('tbody'));

        result
            .attr('id', 'norms_section')
            .append(document.createElement('tr')).children()
                .attr('class', 'sys_clear')
                .append(document.createElement('td')).children()
                    .attr('class', 'form-element-container sys_clear')
                    .append(document.createElement('div')).children()
                        .attr('class', 'form-element')
        return result;
    }

    this.drawReportsTable = function(rows)
    {
        var fff = function(obj)
        {
            var result = $(document.createElement('tbody'));
            for (var i = 0; i < rows.length; i++) {
                var tr = $(document.createElement('tr'));
                var row = rows[i];
            	if (i % 2 != 0) {
            		tr.attr('class', 'altrow');
            	}
            	var input = $('<input type="radio" name="'+obj.Env.select_report_control_name+'" onclick="__ToolGUIEnv.secondOnClick();" value="'+row.id+'" />')
            	tr.append(document.createElement('td')).children()
            	       .attr('class', 'nw')
            	       .text(row.name)
            	   .after(document.createElement('td')).next()
            	       .attr('class', 'nw align-r')
            	       .html(row.html_price)
            	       //.text(row.price)
            	   .after(document.createElement('td')).next()
            	       .attr('class', 'nw')
            	   .append(input);

                result.append(tr);
            }

            return result;
        }

        var result = $(document.createElement('tbody'));

        result
            .attr('id', 'reports_section')
            .append(
                $(document.createElement('tr'))
                    .attr('class', 'sys_clear')
                    .append($(document.createElement('td'))
                        .attr('class', 'form-element-container sys_clear')
                        .append($(document.createElement('div'))
                            .attr('class', 'form-element')
                            .append($(document.createElement('h5'))
                                .attr('class', 'no-margins no-color')
                                .text(this.Env.reports_title)
                            )
                        )
                    )
            )
            .append(document.createElement('tr')).children().next()
                .attr('class', 'sys_clear')
                .append(document.createElement('td')).children()
                    .attr('class', 'form-element-container sys_clear')
                    .append(document.createElement('div')).children()
                        .attr('class', 'form-element non-top-margin')

                        .append(document.createElement('table')).children()
                            .attr('id', 'reports_table')
                            .attr('class', 'table')
                            .attr('cellpadding', '0')

                            .append(document.createElement('thead')).children()
                                .append(document.createElement('tr')).children()
                                    .append(document.createElement('th')).children()
                                        .attr('class', 'nw')
                                        .text(this.Env.reports_headers.name)
                                    .after(document.createElement('th')).next()
                                        .attr('class', 'nw')
                                        .text(this.Env.reports_headers.price)
                                    .after(document.createElement('th')).next()
                                        .attr('class', 'nw actions')
                                    .parent()
                            .parent()
                        .parent()
                            .append(fff(this));

        return result;
    }

    this.drawNormsTable = function(rows)
    {
        var fff = function(obj)
        {
            var result = $(document.createElement('tbody'));
            for (var i = 0; i < rows.length; i++) {
                var tr = $(document.createElement('tr'));
                var row = rows[i];
            	if (i % 2 != 0) {
            		tr.attr('class', 'altrow');
            	}
            	var input = $('<input type="radio" name="'+obj.Env.select_norm_control_name+'" onclick="__ToolGUIEnv.secondOnClick();" value="'+row.id+'" />')
            	tr.append(document.createElement('td')).children()
            	       .attr('class', 'nw')
            	       .text(row.name)
            	   .after(document.createElement('td')).next()
            	       .attr('class', 'nw')
            	   .append(input);

                result.append(tr);
            }

            return result;
        }

        var result = $(document.createElement('tbody'));

        result
            .attr('id', 'norms_section')
            .append(
                $(document.createElement('tr'))
                    .attr('class', 'sys_clear')
                    .append($(document.createElement('td'))
                        .attr('class', 'form-element-container sys_clear')
                        .append($(document.createElement('div'))
                            .attr('class', 'form-element')
                            .append($(document.createElement('h5'))
                                .attr('class', 'no-margins no-color')
                                .text(this.Env.norms_title)
                            )
                        )
                    )
            )
            .append(document.createElement('tr')).children().next()
                .attr('class', 'sys_clear')
                .append(document.createElement('td')).children()
                    .attr('class', 'form-element-container sys_clear')
                    .append(document.createElement('div')).children()
                        .attr('class', 'form-element non-top-margin')

                        .append(document.createElement('table')).children()
                            .attr('id', 'norms_table')
                            .attr('class', 'table')
                            .attr('cellpadding', '0')

                            .append(document.createElement('thead')).children()
                                .append(document.createElement('tr')).children()
                                    .append(document.createElement('th')).children()
                                        .attr('class', 'nw')
                                        .text(this.Env.norms_headers.name)
                                    .after(document.createElement('th')).next()
                                        .attr('class', 'nw actions')
                                    .parent()
                            .parent()
                        .parent()
                            .append(fff(this));

        return result;
    }

    this.getReport = function()
    {
        var selected_id = formValueOf(this.form, this.Env.select_report_control_name);
        typeof selected_id == 'string' ? parseInt(selected_id) : null;

        if (! selected_id) {
            return null;
        }

        var rows = this.getReports();
        if (rows != null) {
            for (var i = 0; i < rows.length; i++) {
                var row = rows[i];
                if (row.id == selected_id) {
                    return row;
                }
            }
        } else {
            return null;
        }
    }

    this.setConfirmString = function()
    {
        var report = this.getReport();
        if (! report) {
            return;
        }
        var confirm_msg = (this.Env.po.confirm_begin + report.txt_price + this.Env.po.confirm_end);
        $(this.submit_button).unbind("click");
        $(this.submit_button).bind("click", function(){if (!confirm(confirm_msg)) return false;});
    }

    this.init = function()
    {
        this.tool_form      = $('#' + this.Env.tool_form_id).get(0);
        this.form           = this.tool_form;
        this.submit_button  = $('#' + this.Env.submit_button_id).get(0);

        this.attacheEvent();
        this.onDraw();
    }

    this.init();
}

var __ToolGUIEnv;